#pragma once

#include <string>
using namespace std;

#include "pugixml/pugixml.hpp"

#include "topcvt_string.hpp"
#include "topcvt_logutil.hpp"
#include "topcvt_exception.hpp"
#include "topcvt_encoding.hpp"

namespace topcvt
{
	struct xml_string_writer : pugi::xml_writer
	{
		string strXmlMsg;
		virtual void write(const void* data, size_t size)
		{
			strXmlMsg += string(static_cast<const char*>(data), size);
		};
	};

	enum class XmlNodeType
	{
		element,
		attribute
	};

	class XmlHelper
	{
	public:
		static pugi::xml_document& parseXml(pugi::xml_document& doc, const string& xmlMessage);
		static void saveXml(pugi::xml_document& doc, string& xmlMessage);
		static bool validateXmlByXsd(const string& xmlMessage,const string& xsdfile);
		static string select_node(pugi::xml_node& xmlnode, const string& xpath);
		static pugi::xpath_node_set& select_nodes(pugi::xml_node& xmlnode, const string& xpath, pugi::xpath_node_set& xpath_nodes);
		static XmlNodeType create_node(pugi::xml_node& xmlnode, const string& xpath, pugi::xml_node& newNode, string& out_value, bool repeatable = false);
		static void appendDeclaration(pugi::xml_document& doc, const string& version="1.0", const string& encoding="GBK");
		static bool checkXpathValid(const string& xpath);
		static size_t childCounts(const pugi::xml_node& node, const string& name);
	};
};